$(function () {
    $("#jqGrid").Grid({
        url: '../textconfig/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '标题', name: 'title', index: 'title', width: 80},
            {label: '内容', name: 'content', index: 'content', width: 80},
            {label: '底部', name: 'bottom', index: 'bottom', width: 80},
            {
                label: '类型', name: 'type', index: 'type', width: 20, formatter: function (value) {
                    if (value == 1) {
                        return '邀请';
                    } else if (value == 2) {
                        return '分享';
                    } else if (value == 3) {
                        return '闪兑';
                    } else if (value == 7) {
                        return '报名';
                    } else if (value == 5) {
                        return '竞拍';
                    } else if (value == 6) {
                        return '抢购';
                    }
                    return "其他";
                }
            },
            {label: '描述说明', name: 'comment', index: 'comment', width: 80},
            {label: 'Done文字', name: 'done', index: 'done', width: 30},
            {label: '非Done文字', name: 'nodone', index: 'nodone', width: 30},
            {label: 'html内容', name: 'htmlComment', index: 'htmlComment', width: 80},
            {label: '创建时间', name: 'createDate', index: 'create_date', width: 40}]
    });
    $('#htmlComment').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '500px', //高度
        minHeight: '200px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload?prefix=study',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    })
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        textConfig: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.textConfig = {};
            $('#htmlComment').editable('setHTML', '');
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.textConfig.id == null ? "../textconfig/save" : "../textconfig/update";
            vm.textConfig.htmlComment = $('#htmlComment').editable('getHTML');
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.textConfig),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../textconfig/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../textconfig/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.textConfig = r.textConfig;
                    $('#htmlComment').editable('setHTML', vm.textConfig.htmlComment);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },

        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
    }
});